package su.samcom.calc.model.dao;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import su.samcom.calc.model.entity.Property;

import javax.persistence.Entity;
import java.io.Serializable;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Владислав
 * Date: 23.11.13
 * Time: 20:55
 * To change this template use File | Settings | File Templates.
 */
public abstract class BaseDaoImpl<E extends Serializable> implements BaseDao<E> {

    @Autowired
    SessionFactory sessionFactory;

    protected abstract Class<E> getGeneric();

    @Override
    public E getById(Long id) {
        return (E) sessionFactory.getCurrentSession().get(getGeneric(), id);
    }

    @Override
    public List<E> getAll() {
        String sql = String.format("from %s e", getGeneric().getSimpleName());
        return sessionFactory.getCurrentSession().createQuery(sql).list();
    }
}
